package com.zhugang.week01;


/**
 * @program algorithms
 * @description: 左旋字符串
 * @author: chanzhugang
 * @create: 2022/03/23 07:34
 */
public class ReverseLeftWords {

    public static void main(String[] args) {
        ReverseLeftWords leftWords = new ReverseLeftWords();
        String s = "abcdefg";
        String str = leftWords.reverseLeftWord(s, 2);
        System.out.println(str);
    }

    /**
     * 左旋字符串:  abcdefg 2 - > cdefgab
     *
     * @param s
     * @param n
     * @return
     */
    public String reverseLeftWord(String s, int n) {
        // 先把 0-n位 挪到尾部
        char[] temp = new char[s.length()];
        for (int i = 0; i < n; i++) {
            temp[i + (s.length() - n)] = s.charAt(i);
        }
        // 再把 n-len-1 挪到前面
        for (int i = n; i < s.length(); i++) {
            temp[i - n] = s.charAt(i);
        }
        return new String(temp);
    }
}